package com.xyzh05.webapp.action;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.service.SchoolInfoManager;
import com.xyzh05.model.SchoolInfo;
import com.xyzh05.webapp.action.BaseAction;

import java.util.List;

public class SchoolInfoAction extends BaseAction implements Preparable {
    private SchoolInfoManager schoolInfoManager;
    private List schoolInfoes;
    private SchoolInfo schoolInfo;
    private Long id;
    private String query;

    public void setSchoolInfoManager(SchoolInfoManager schoolInfoManager) {
        this.schoolInfoManager = schoolInfoManager;
    }

    public List getSchoolInfoes() {
        return schoolInfoes;
    }

    /**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String schoolInfoId = getRequest().getParameter("schoolInfo.id");
            if (schoolInfoId != null && !schoolInfoId.equals("")) {
                schoolInfo = schoolInfoManager.get(new Long(schoolInfoId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
        
        return "list";
    }

    public void setId(Long id) {
        this.id = id;
    }

    public SchoolInfo getSchoolInfo() {
        return schoolInfo;
    }

    public void setSchoolInfo(SchoolInfo schoolInfo) {
        this.schoolInfo = schoolInfo;
    }

    public String delete() {
        schoolInfoManager.remove(schoolInfo.getId());
        saveMessage(getText("schoolInfo.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            schoolInfo = schoolInfoManager.get(id);
        } else {
            schoolInfo = new SchoolInfo();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (schoolInfo.getId() == null);

        schoolInfoManager.save(schoolInfo);

        String key = (isNew) ? "schoolInfo.added" : "schoolInfo.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
}